// pages/user-info/user-info.js
var api = require('../../config/api.js');
var util = require('../../utils/util.js');
import Dialog from 'vant-weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showUpdateName: false,
    showSex: false,
    showBirthday: false,
    actions: [{
        name: '男',
      },
      {
        name: '女',
      }
    ],
    currentDate: new Date().getTime(),
    minDate: Date.parse('1900/01/01'),
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      } else if (type === 'month') {
        return `${value}月`;
      }
      return value;
    },
    name: '',
    userName: '',
    userSex: '',
    userTel: '',
    userBirthday: ''
  },
  //跳转到绑定手机
  onBindTel(e) {
    wx.navigateTo({
      url: '/pages/bind-tel/bind-tel',
    })
  },
  //修改姓名弹窗
  onUpdateName(e) {
    this.setData({
      showUpdateName: true
    })
  },
  //获取输入框姓名
  getName(e) {
    var name = e.detail.value;
    this.setData({
      name: name
    })
  },
  //确认修改
  onSureName(e) {
    var name = this.data.name
    if (util.isEmpy(name)) {
      util.myToast("请输入姓名")
    } else {
      this.getUpData('', name, '');
    }
    this.setData({
      showUpdateName: false
    })
  },
  onCloseName(e) {
    this.setData({
      showUpdateName: false
    })
  },
  //修改性别弹窗
  onChooseSex(e) {
    this.setData({
      showSex: true
    })
  },
  onCloseSex(e) {
    this.setData({
      showSex: false
    })
  },
  //选择性别
  onSelectSex(e) {
    console.log("xingbie", e.detail)
    var sex = e.detail.name == '男' ? 1 : 2
    this.getUpData(sex, '', '');
  },
  //选择生日
  onChooseBirthday(e) {
    console.log(new Date().getTime())
    this.setData({
      showBirthday: true
    })
  },
  //关闭生日
  onCloseBirthday(e) {
    this.setData({
      showBirthday: false
    })
  },
  //日期发生改变时
  onInput(event) {
    this.setData({
      currentDate: event.detail,
    });
  },
  //选择日期
  onSureBirhtday(e) {
    this.setData({
      showBirthday: false
    })

    var birthday = util.formatTime(e.detail, "Y/M/D");
    this.getUpData('', '', birthday)
  },
  //提货地址
  onChooseAddress(e) {
    wx.navigateTo({
      url: '/pages/address/address',
    })
  },
  //退出登录
  onLogout(e) {
    wx.showModal({
      title: '',
      content: '退出登录？',
      success: function(res) {
        if (!res.confirm) {
          return;
        }
        wx.clearStorageSync();

        wx.reLaunch({
          url: '/pages/index/index'
        });
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },
  //获取数据
  getData() {
    let that = this;
    util.request(api.Center).then(function(res) {
      if (res.errno == 0) {
        that.setData({
          userInfo: res.data.user,
          userName: res.data.user.username,
          userSex: res.data.user.gender == 1 ? '男' : '女',
          userTel: util.isEmpy(res.data.user.mobile) ? '未设置' : res.data.user.mobile,
          userBirthday: util.isEmpy(res.data.user.birthday) ? '未设置' : res.data.user.birthday,
        })
      } else {
        util.myToast(res.errmsg)
      }
    });
  },
  //修改个人信息
  getUpData(sex, name, birthday) {
    let that = this;
    util.request(api.Profile, {
      sex: sex,
      name: name,
      birthday: birthday
    }, 'post').then(function(res) {
      if (res.errno == 0) {
        if (name != '') {
          that.setData({
            userName: name
          })
        }
        if (sex != '') {
          that.setData({
            userSex: sex == 1 ? '男' : '女'
          })
        }
        if (birthday != '') {
          that.setData({
            userBirthday: birthday
          })
        }

      } else {
        util.myToast(res.errmsg)
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})